package com.segersten.othello.service.rest;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlRootElement;

import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.segersten.othello.service.akka.lobby.BoardReference;
import com.segersten.othello.service.akka.lobby.Game;
import com.segersten.othello.service.akka.lobby.LobbyState;
import com.segersten.othello.service.akka.lobby.Ticket;

@XmlRootElement
public class LobbyStateResponse
{
    private static final Function<Game, Game> TRANSFORM = new Function<Game, Game>()
    {

        @Override
        public Game apply(Game game)
        {
            return new Game(new BoardReference("/game/othello2/board/" +game.getBoardReference().getReference()), 
                        game.getPlayers());
        }
    };
    Ticket ticket;
    List<Game> games;

    public LobbyStateResponse()
    {
    }

    public LobbyStateResponse(Ticket ticket, List<Game> games)
    {
        this.ticket = ticket;
        this.games = games;
    }

    public static LobbyStateResponse from(LobbyState state)
    {
        return new LobbyStateResponse(state.getTicket(), Lists.newArrayList(Collections2.transform(state.getGames(), TRANSFORM)));
    }
    
    

    public Ticket getTicket()
    {
        return ticket;
    }

    public void setTicket(Ticket ticket)
    {
        this.ticket = ticket;
    }

    public List<Game> getGames()
    {
        return games == null ? new ArrayList<Game>() : games;
    }

    public void setGames(List<Game> games)
    {
        this.games = games;
    }

    @Override
    public String toString()
    {
        return "LobbyStateResponse [ticket=" + ticket + ", games=" + games + "]";
    }
    
}
